<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增公司')"/>
    <th:block th:include="include :: select2-css"/>
</head>
<body>
<div class="main-content">
    <form class="form-horizontal" id="form-user-add">
        <h4 class="form-header h4">基本信息</h4>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">收购公司：</label>
                    <div class="col-sm-8">
                        <input id="companyName" name="companyName" placeholder="請輸入收购公司" class="form-control" type="text" maxlength="30"
                               required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">收购品名：</label>
                    <div class="col-sm-8">
                        <input id="receiptMaterial" name="receiptMaterial" placeholder="請輸入收购品名" class="form-control" type="text"
                               maxlength="30" required>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">收購總量：</label>
                    <div class="col-sm-8">
                        <input id="receiptNum" name="receiptNum" placeholder="請輸入收購總量" class="form-control" type="text" maxlength="30"
                               required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">合同信息：</label>
                    <div class="col-sm-8">
                        <input id="filePath" name="filePath" class="form-control" type="file">
                        <!--                        <input name="receiptContract" placeholder="請輸入合同信息" class="form-control" type="file" maxlength="30" required>-->
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">合同協議號：</label>
                    <div class="col-sm-8">
                        <input name="contractNo" placeholder="請輸入合同協議號" class="form-control" type="text" maxlength="30"
                               required>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">商品編碼：</label>
                    <div class="col-sm-8">
                        <input name="saleNo" placeholder="請輸入商品編碼" class="form-control" type="text" maxlength="30"
                               >
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">合同有效期：</label>
                    <div class="col-sm-8">
                        <input type="text" class="time-input" id="contractTime" name="contractTime" placeholder="請選擇合同有效期"/>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label">單位：</label>
                    <div class="col-sm-8">
                        <select id="unit" name="unit" class="form-control m-b"
                                th:with="type=${@dict.getType('sys_leftover_unit')}">
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                                    required></option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存
        </button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭
        </button>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<script>
    var prefix = ctx + "system/company";
    var filePath = "";

    $("#form-user-add").validate({
        onkeyup: false,
        rules: {},
        focusCleanup: true
    });

    function uploadFile() {
        var formData = new FormData();
        if ($('#filePath')[0].files[0] == null) {
            $.modal.alertWarning("请先选择文件路径");
            return false;
        }
        // var data = $("#form-user-add").serializeArray();
        formData.append('companyName', $("#companyName").val());
        formData.append('receiptMaterial', $("#receiptMaterial").val());
        formData.append('receiptNum', $("#receiptNum").val());
        formData.append('contractTime', $("#contractTime").val());
        formData.append('contractNo', $("#contractNo").val());
        formData.append('saleNo', $("#saleNo").val());
        formData.append('unit', $("#unit").val());
        formData.append('fileName', $("#fileName").val());
        formData.append('file', $('#filePath')[0].files[0]);
        $.ajax({
            url: prefix + "/add",
            type: 'post',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (result) {
                // $.operate.ajaxSuccess(result);
                result.code = 0;
                $.operate.successTabCallback(result);
                filePath = result.msg;
                return result.msg;
            }
        });
    }

    function submitHandler() {
        if ($.validate.form()) {
            var b = uploadFile();
            // var data = $("#form-user-add").serializeArray();
            // data.push({"name": "receiptContract", "value": filePath});
            // $.operate.saveTab(prefix + "/add", data);
        }
    }
</script>
</body>
</html>